﻿using github.heerheer.card2.Code.Events;
using Native.Sdk.Cqp.EventArgs;
using Native.Sdk.Cqp.Interface;
using System;

namespace Code
{
    public class ReceiveGroupMessage : IGroupMessage
    {
        public void GroupMessage(object sender, CQGroupMessageEventArgs e)
        {
            try
            {
                if (    
                    AppMain.GobalSetting==null 
                    ||!AppMain.GobalSetting.enableApp
                    || MessageEvents.MessageEventsContainer==null
                    || AppMain.Pools==null
                    )
                {
                    e.Handler=false;
                    return;
                }
                //群控制
                if (AppMain.GobalSetting.allowGroups != "" && AppMain.GobalSetting.allowGroups != null)
                {
                    //设置了

                    if (!AppMain.GobalSetting.allowGroups.Contains(";"))
                    {
                        //没有分割
                        if (e.FromGroup.Id.ToString() != AppMain.GobalSetting.allowGroups)
                        {
                            e.Handler = false;
                            return;
                        }
                    }
                    else
                    {
                        //有分割
                        var gs = AppMain.GobalSetting.allowGroups.Split(';');
                        if (Array.IndexOf(gs, e.FromGroup.Id.ToString()) == -1)
                        {
                            //不存在这个群
                            e.Handler = false;
                            return;
                        }
                    }
                }
                //寻找相对应事件
                foreach (var item in MessageEvents.MessageEventsContainer)
                {
                    if (item.Key == e.Message)
                    {
                        item.Value.Process(e);
                        e.Handler = true;
                        return;
                    }
                }
                if (!AppMain.GobalSetting.enableApp)
                {
                    e.Handler = false;
                    return;
                }

                //没找到对应的

                //找卡池
                foreach (var item in AppMain.Pools)
                {
                    if (e.Message == item.Message)
                    {
                        Event_drawCard.draw(item, e);
                        e.Handler = true;
                        return;
                    }
                    if (e.Message == item.TenMessage)
                    {
                        Event_drawCard.draw(item, e, 10);
                        e.Handler = true;
                        return;
                    }
                }
                //卡池里也没有
                //空格解析是不是要多次...
                if (e.Message.Text.Contains(" "))
                {
                    //e.CQLog.Info("233", e.Message.Text.Split(' ')[0]);
                    var mess = e.Message.Text.Split(' ')[0];
                    var mess2 = e.Message.Text.Split(' ')[1];
                    var count = 0;
                    if (int.TryParse(mess2.Trim(), out count))
                    {
                        foreach (var item in AppMain.Pools)
                        {
                            if (mess == item.Message && count<=40)
                            {
                                e.CQLog.Debug("","触发了多次");
                                Event_drawCard.draw(item, e,count);
                                e.Handler = true;
                                return;
                            }else if ( mess==item.Message && count > 40)
                            {
                                e.FromGroup.SendGroupMessage("请不要进行大于40次的行为");
                            }
                        }
                    }
                    else
                    {
                        e.CQLog.Debug("", mess2);

                    }
                }

                if (e.Message == "赫尔小姐姐还在吗，想你了")
                {
                    e.FromGroup.SendGroupMessage("在呢在呢，来玩啊");
                    e.Handler = true;
                }

                //经济相关

                if(e.Message.Text.Split(' ')[0] == AppMain.commandHelper.EcoCommand)
                {
                    new Event_Eco().Process(e);
                    e.Handler = true;

                }
                e.Handler = false;
            }
            catch (Exception ex)
            {
                //e.FromGroup.SendGroupMessage("错误:\n" + ex.ToString());
                e.CQLog.Warning("消息处理逻辑", ex.Message);
                e.CQLog.Debug("Info", ex.StackTrace + ex.Source);
            }
        }
    }
}